﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace AzureStorageLight.Tests
{
    [TestClass]
    public class StorageItemManagerTest
    {
        StorageItemManager Manager;


        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            var Manager = new StorageItemManager(
                System.Configuration.ConfigurationSettings.AppSettings["BlobConnection"]
                , "DirectoryFile", "utest1017", "utest1017bk");

            Manager.EnsureItem("/UnitTest/").Wait();
            Manager.EnsureItem("/UnitTest/folderA/").Wait();
            Manager.EnsureItem("/UnitTest/folderB/").Wait();
            Manager.EnsureItem("/UnitTest/folderB/folderB1/").Wait();
            Manager.EnsureItem("/UnitTest/folderC/").Wait();
            var aFs = Path.GetTempFileName();
            try
            {
                File.WriteAllText(aFs, "TEST");
                Manager.UploadAsync("/UnitTest/a.txt", aFs).Wait();
                Manager.UploadAsync("/UnitTest/folderB/a.txt", aFs).Wait();
            }
            finally
            {
                File.Delete(aFs);
            }
        }
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
        }

        [TestInitialize]
        public void TestSetUp()
        {
            Manager = new StorageItemManager(
                System.Configuration.ConfigurationSettings.AppSettings["BlobConnection"]
                , "DirectoryFile", "utest1017", "utest1017bk");
        }

        [TestCleanup]
        public void TestCleanup()
        {
        }



        [TestMethod]
        public void GetFilesTest()
        {
            var list = Manager.GetFiles("/UnitTest/");
            Assert.AreEqual(4, list.Count);
        }

        [TestMethod]
        public void DownloadTest()
        {
            var aFs = Path.GetTempFileName();
            try
            {
                Manager.DownloadAsync("/UnitTest/folderB/a.txt", aFs).Wait();
                Assert.AreEqual("TEST", File.ReadAllText(aFs));
            }
            finally
            {
                File.Delete(aFs);
            }
        }

        [TestMethod]
        public void MoveTest()
        {
            var aFs = Path.GetTempFileName();
            try
            {
                File.WriteAllText(aFs, "TEST");
                Manager.UploadAsync("/UnitTest/folderB/b.txt", aFs).Wait();
                Manager.MoveAsync("/UnitTest/folderB/b.txt", "/UnitTest/folderC/c.txt");
                Manager.DownloadAsync("/UnitTest/folderC/c.txt", aFs).Wait();
                Assert.AreEqual("TEST", File.ReadAllText(aFs));
            }
            finally
            {
                File.Delete(aFs);
            }
        }

        [TestMethod]
        public void UploadTest()
        {
            var aFs = Path.GetTempFileName();
            var bFs = Path.GetTempFileName();
            try
            {
                File.WriteAllText(aFs, "TEST");
                Manager.UploadAsync("/UnitTest/folderC/test.txt", aFs).Wait();
                Manager.DownloadAsync("/UnitTest/folderC/test.txt", bFs).Wait();
                Assert.AreEqual("TEST", File.ReadAllText(bFs));
            }
            finally
            {
                File.Delete(aFs);
                File.Delete(bFs);
            }
        }

        [TestMethod]
        public void DeleteTest()
        {
            var aFs = Path.GetTempFileName();
            var bFs = Path.GetTempFileName();
            try
            {
                File.WriteAllText(aFs, "Temp");
                Manager.UploadAsync("/UnitTest/folderC/temp.txt", aFs).Wait();
                Manager.DeleteAsync("/UnitTest/folderC/temp.txt").Wait();
            }
            finally
            {
                File.Delete(aFs);
                File.Delete(bFs);
            }
        }

        [TestMethod]
        public void BackupTest()
        {
            var aFs = Path.GetTempFileName();
            try
            {
                File.WriteAllText(aFs, "Temp");
                for (int i=0; i<10; i++)
                {
                    Manager.UploadAsync("/UnitTest/folderC/temp.txt", aFs).Wait();
                    Manager.BackupAsync().Wait();
                }
                Manager.PurgeBackupAsync(new TimeSpan[] { new TimeSpan(0, 1, 0), new TimeSpan(1, 0, 0), new TimeSpan(1,0,0,0,0), }).Wait();
                var temp =Manager.ListBackup("/UnitTest/folderC/temp.txt", true);
                Assert.IsTrue(1 <= temp.Count);
            }
            finally
            {
                File.Delete(aFs);
            }
        }

        [TestMethod]
        public void CraeteTicketTest()
        {
            var ticket = StorageItemManager.CreateTicket(System.Configuration.ConfigurationSettings.AppSettings["BlobConnection"]
                , "DirectoryFile", "utest1017", "utest1017bk", DateTime.Now.AddSeconds(5), "P@ssw0rd");

            var config = new StorageConfig();
            config.Ticket = CryptUtil.DecryptStringAES(ticket, "P@ssw0rd");
            var mng = StorageItemManager.CreateStorageItemManager(config);
            mng.GetFiles("/");
        }
    }
}
